/*
 *  SyncIT , easy synchronization of files between computers and devices.
 *  Copyright (C) 2011  Christian Masus <cmasus(at)gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version. If redistributing and/or modifying
 *  this file, this notice must be included in the redistributed or
 *  modified copy.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package gui.javagui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

/**
 * Opens a JDialog which displays the about info to this application.
 * @author Christian Masus
 *
 */
public class DialogAbout {

	private final JDialog dialog;
	private JButton okButton;

	/**
	 * Initiates and displays this JDialog.
	 */
	public DialogAbout(String version) {
		JFrame frame = JavaGui.getFrame();
		dialog = new JDialog(frame, "About SyncIT");


		JPanel iconPanel = new JPanel();
		iconPanel.setLayout(new BoxLayout(iconPanel, BoxLayout.Y_AXIS));
		iconPanel.setBorder(BorderFactory.createEmptyBorder(0,7,0,0));
		Icon icon = new ImageIcon("res/SyncIT_128.png");
		JLabel iconLabel = new JLabel(icon);
		iconPanel.add(Box.createVerticalGlue());
		iconPanel.add(iconLabel);
		iconPanel.add(Box.createVerticalGlue());


		JPanel infoPanel = new JPanel();
		infoPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
		JLabel infoLabel = new JLabel("<html><h1>SyncIT " + version + "</h1>" +
				"<p><p>SyncIT is a simple synchronization program" +
				"<p>that lets you synchronize files and folders" +
				"<p>between different computers and devices," +
				"<p>no matter what OS is used. As long as the" +
				"<p>system supports Java.<p>" +
				"<p>Copyright &copy; 2011 Christian Masus</html>"
		);
		infoPanel.add(infoLabel);


		JPanel buttonPanel = new JPanel();
		okButton = new JButton(closeAction);
		buttonPanel.add(okButton);


		JPanel contentPanel = new JPanel(new BorderLayout());
		contentPanel.add(iconPanel, BorderLayout.WEST);
		contentPanel.add(infoPanel, BorderLayout.CENTER);
		contentPanel.add(buttonPanel, BorderLayout.SOUTH);
		contentPanel.setOpaque(true);
		dialog.setContentPane(contentPanel);


		dialog.pack();
		dialog.setResizable(false);
		dialog.setLocationRelativeTo(frame);

		dialog.getRootPane().setDefaultButton(okButton);
		okButton.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "close");
		okButton.getActionMap().put("close", closeAction);
		okButton.requestFocusInWindow();

		dialog.setVisible(true);
	}


	/**
	 * Action for closing this dialog.
	 */
	@SuppressWarnings("serial")
	Action closeAction = new AbstractAction("OK") {
		public void actionPerformed(ActionEvent e) {
			dialog.setVisible(false);
			dialog.dispose();
		}
	};

}
